package com.xiaoluyy.demo.common.util;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.TypeReference;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/10/12.
 */
public class JsonUtil {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    public JsonUtil() {
    }

    public static String toJson(Object obj) {
        if(obj == null) {
            return null;
        } else {
            try {
                return MAPPER.writeValueAsString(obj);
            } catch (Exception var2) {
                throw new RuntimeException("error encode json for " + obj, var2);
            }
        }
    }

    public static byte[] toBytes(Object obj) {
        try {
            return MAPPER.writeValueAsBytes(obj);
        } catch (Exception var2) {
            throw new RuntimeException("error encode json for " + obj, var2);
        }
    }

    public static <T> T toObject(String json, Class<T> cls) {
        if(json == null) {
            return null;
        } else {
            try {
                return MAPPER.readValue(json, cls);
            } catch (Exception var3) {
                throw new RuntimeException("error decode json to " + cls, var3);
            }
        }
    }

    public static <T> T toObject(byte[] jsonBytes, Class<T> cls) {
        try {
            return MAPPER.readValue(jsonBytes, cls);
        } catch (Exception var3) {
            throw new RuntimeException("error decode json to " + cls);
        }
    }

    public static <T> T toObject(String json, TypeReference typeReference) {
        try {
            return MAPPER.readValue(json, typeReference);
        } catch (Exception var3) {
            throw new RuntimeException("error decode json to " + typeReference);
        }
    }

    public static Map<String, Object> readMap(String json) {
        try {
            return (Map)MAPPER.readValue(json, HashMap.class);
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    static {
        MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MAPPER.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    }
}
